using System.Reflection;
using KL.Standard.Core.Utilities.Excel;
using Newtonsoft.Json;

namespace KL.Standard.Model.Infrastructure;

/// <summary>
/// 分页信息
/// </summary>
/// <typeparam name="T"></typeparam>
public class PageResult<T>
{
    #region 构建栏位信息

    /// <summary>
    /// 构建栏位信息
    /// </summary>
    public void CreateColumnInfos(int defaultWidth)
    {
        this.columns = new List<VTableColumnInfo>();
        var type = typeof(T);
        var properties = type.GetProperties();
        int index = 0;
        foreach (var item in properties)
        {
            var attribute = item.GetCustomAttributes<VTabClmAttribute>(true).FirstOrDefault();
            if (attribute != null)
            {
                columns.Add(new VTableColumnInfo()
                {
                    Prop = item.Name,
                    Label = attribute.ClmLabel,
                    Width = attribute.ClmWidth ?? defaultWidth,
                    OrderSeq = attribute.OrderSeq ?? index,
                    IsFilt = attribute.IsFilt4Clm,
                    Bg = attribute.ClmBg
                });
            }

            index++;
        }

        this.columns = this.columns.OrderBy(i => i.OrderSeq).ToList();
    }

    #endregion



    /// <summary>
    /// 第几页
    /// </summary>
    public int pageIndex { set; get; }

    /// <summary>
    /// 每页数量
    /// </summary>
    public int pageSize { set; get; }

    /// <summary>
    /// 总共数量
    /// </summary>
    public int totalCount { get; set; }

    /// <summary>
    /// 数据集合
    /// </summary>
    public IEnumerable<T> rows { get; set; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public IList<VTableColumnInfo> columns { private set; get; }
}

/// <summary>
/// VUE TABLE COLUMN
/// </summary>
public class VTableColumnInfo
{
    /// <summary>
    /// 属性
    /// </summary>
    public string Prop { set; get; }

    /// <summary>
    /// 标题
    /// </summary>
    public string Label { set; get; }

    /// <summary>
    /// 宽度
    /// </summary>
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? Width { set; get; }

//[JsonIgnore]
    public int OrderSeq { set; get; }

    public bool IsFilt { set; get; }

    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string Bg { set; get; }

}


public class PageSearchSortModel
{
    public string Sort { get; set; }
    public int Offset { get; set; }
    public int Limit { get; set; }
}